package GUI;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;

import scale.data.Scale;
import scale.sim.ScaleSimApp;

import ase.ASEStarter;


public class RealWeightIP  {
	
	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	JTextArea T1;
	JTextArea T2;
	JTextArea T3;
	JButton b = new JButton();
	JFrame f = new JFrame();
	
	public RealWeightIP(){
		f.setLayout(new GridBagLayout());
		f.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		f.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		Border border = BorderFactory.createLineBorder(Color.BLACK);
		
		
//		Info text panel:
		T1 = new JTextArea();
		
		T1.setEditable(false);
		T1.setFont(new Font("",Font.PLAIN, 20));
		T1.setMaximumSize(T1.getPreferredSize());
		T1.setMinimumSize(T1.getPreferredSize());
		T1.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createTitledBorder("Info")));
		T1.setText("Enter IP and Port number to connect to the weight.");
		c.anchor = GridBagConstraints.FIRST_LINE_START;
		c.anchor = GridBagConstraints.FIRST_LINE_START;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.ipadx = 300;
		c.ipady = 20;
		f.add(T1,c);
		
//		Ip input panel:
		T2 = new JTextArea();
		T2.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createTitledBorder("IP-adresse")));
		T2.setFont(new Font("",Font.PLAIN, 20));
		T2.setMaximumSize(T2.getPreferredSize());
		T2.setMinimumSize(T2.getPreferredSize());
		c.gridx = 0;
		c.gridy = 1;
		c.ipadx = 200;
		c.ipady = 20;
		f.add(T2,c);
		
//		port nr. textfield:
		T3 = new JTextArea(1,4);
		T3.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createTitledBorder("Port-nr")));
		T3.setFont(new Font("",Font.PLAIN, 20));
		T3.setMaximumSize(T3.getPreferredSize());
		T3.setMinimumSize(T3.getPreferredSize());
		c.gridx = 1;
		c.gridy = 1;
		c.ipadx = 50;
		c.ipady = 20;
		f.add(T3,c);
		
//		Connector button:
		b = new JButton("Connect");
		c.gridx = 2;
		c.gridy = 0;
		c.weightx = 0.5;
		c.weighty = 0.5;
		c.ipadx = 40;
		c.ipady = 40;
		b.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				try {
					new Thread(new ASEStarter(getIp(), Integer.parseInt(getPort().toString()))).start();
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(f,
						    "Entered IP/port number is invalid! ",
						    "Port Error",
						    JOptionPane.ERROR_MESSAGE);					e1.printStackTrace();
				}
			}
		}
			
		);
		f.add(b,c);
		
//		Simulator button
		b = new JButton("Start Simulator");
		c.gridx = 2;
		c.gridy = 1;
		c.weightx = 0.5;
		c.weighty = 0.5;
		c.ipadx = 40;
		c.ipady = 40;
		b.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ScaleSimApp S = new ScaleSimApp();
				S.main(null);
				new Thread(new ASEStarter("127.0.0.1", 8000)).start();
				f.setVisible(false);
				
			}
			
		});
		f.add(b,c);
		
////		Cancel button:
//		b = new JButton("Cancel");
//		c.gridx = 2;
//		c.gridy = 2;
//		c.ipadx = 20;
//		c.ipady = 20;
//		c.weightx = 0.5;
//		c.weighty = 0.5;
//		b.addActionListener(new ActionListener() {
//			
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				System.exit(0);
//				
//			}
//		});
//		
//		f.add(b,c);
		f.pack();
		f.setLocation((dim.width / 2) - (f.getSize().width/2), (dim.height / 2 - f.getSize().height/2 ));
		f.setVisible(true);
		f.setResizable( false );
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	public String getIp(){
		return T2.getText();
	}
	public String getPort(){
		return T3.getText();
	}
	
	
	public void setInfoText(String S){
		T1.setText(S);
	}
}
